package so.shanku.essential.adapter;

import android.content.Context;
import android.text.TextPaint;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import aym.util.json.JsonMap;
import com.cnfol.emoj.bean.LiveColorBean;
import com.cnfol.emoj.util.MyLiveTextView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.nostra13.universalimageloader.core.ImageLoader;
import so.shanku.essential.R;
import so.shanku.essential.app.MyApplication;
import so.shanku.essential.utils.ImageLoaderBitmapCallBack;
import so.shanku.essential.utils.LogUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Jaelyn on 16/2/24.
 * 发现搜索
 */
public class FindSearchAdapter extends MyBaseAdapter {
    /**
     * 标记特殊的关键字
     */
    private String specialStr;
    private LiveColorBean liveColorBean;
    private ArrayList<LiveColorBean> list;

    public FindSearchAdapter(Context context) {
        super(context);
    }

    public FindSearchAdapter(Context context, List<? extends Map<String, ?>> datas) {
        super(context, datas);
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        FindSearchAdapterViewHolder findSearchAdapterViewHolder;
        if (view == null) {
            view = layoutInflater.inflate(R.layout.find_search_item, null);
            findSearchAdapterViewHolder = new FindSearchAdapterViewHolder();
            ViewUtils.inject(findSearchAdapterViewHolder, view);
            view.setTag(findSearchAdapterViewHolder);
        } else {
            findSearchAdapterViewHolder = (FindSearchAdapterViewHolder) view.getTag();
        }
        bindData(i, findSearchAdapterViewHolder);
        return view;
    }


    private void bindData(final int i, FindSearchAdapterViewHolder viewHolder) {
        JsonMap jsonMap = (JsonMap) datas.get(i);

        String picPath = jsonMap.getStringNoNull("MainImage");
        if (MyApplication.getInstance().getBitmapHashMap().containsKey(picPath)) {
            viewHolder.artical_logo_iv.setImageBitmap(MyApplication.getInstance().getBitmapHashMap().get(picPath));
        } else {
            ImageLoader.getInstance().displayImage(picPath, viewHolder.artical_logo_iv, MyApplication.getInstance().getDefaultOption(), new ImageLoaderBitmapCallBack());
        }

        viewHolder.artical_title_iv.insertGif(jsonMap.getStringNoNull("Title"), list, new MyLiveTextView.SetSpanListener() {
            @Override
            public void updateDrawState(TextPaint ds) {
                LogUtil.d("sss","click");
            }

            @Override
            public void onClick(View view, LiveColorBean liveColorBean) {
                LogUtil.d("sss","click");
            }
        });
    }

    public void setSpecialStr(String specialStr) {
        this.specialStr = specialStr;
        liveColorBean = new LiveColorBean(specialStr, "#0000ff", "");
        list = new ArrayList<LiveColorBean>();
        list.add(liveColorBean);

    }


    public class FindSearchAdapterViewHolder {
        @ViewInject(R.id.artical_logo_iv)
        public ImageView artical_logo_iv;

        @ViewInject(R.id.artical_title_tv)
        public MyLiveTextView artical_title_iv;

        @ViewInject(R.id.artical_disc_tv)
        public MyLiveTextView artical_disc_tv;

        @ViewInject(R.id.classfy_name_tv)
        public TextView classfy_name_tv;

        @ViewInject(R.id.author_tv)
        public MyLiveTextView author_tv;

        @ViewInject(R.id.create_time_tv)
        public TextView create_time_tv;


    }
}
